<?php 
	require_once("CommonAction.php");
	class EditeurAction extends CommonAction {
	
		public function __construct() {
			parent::__construct();
		}
		
		public function execute() {
			parent::execute();
			if (!$this->login()){
				unset($_SESSION["login"]);
				header("Location:index.php");
			}
			
			$this->deleteBD();
			
			$this->ajouterBDEquipe();
			$this->updateBDEquipe();
			
			$this->ajouterBDCarrieres();
			$this->updateBDCarrieres();
			
			$this->ajouterBDServices();
			$this->updateBDServices();
			
		}
		
		public function login(){
			if (isset($_POST["un"]) && isset($_POST["pw"])){
				if (UserDAO::login($_POST["un"], $_POST["pw"])){
					$_SESSION["login"]="logged";
					return true;
				}
				else{
					return false;
				}
			}
			else if (strcmp($_SESSION["login"],"logged")==0){
					return true;
			}
			else{
				return false;
			}
		}
		
		public function deleteBD(){
			if (isset($_POST["id"]) && isset($_POST["table"]) && isset($_POST["supprimer"]))  {				
				$id=$_POST["id"];
				$table=Donnees::$tables[$_POST["table"]];
				parent::deleteBD($id,$table);
			}
			
		}
		
		
		public function ajouterBDEquipe(){
			
			if (isset($_POST["prenom"]) && isset($_POST["nom"]) && isset($_POST["ajouter"])){
				$valeurs=array($_POST["prenom"],$_POST["nom"],$_POST["poste"],$_POST["description"],$_POST["image"]);
				$table="PHP_EQUIPE";
				
				if (strcmp($_SESSION["lang"],"fr")==0){
					parent::insertBD(Donnees::$equipeFR ,$valeurs,$table);	
				}
				else{
					parent::insertBD(Donnees::$equipeEN ,$valeurs,$table);
				}					
			}			
			
			else{
				return false;
			}			
			
		}
		
		public function updateBDEquipe(){
			
			if (isset($_POST["prenom"]) && isset($_POST["nom"]) && isset($_POST["id"])){
				$valeurs=array($_POST["id"],$_POST["prenom"],$_POST["nom"],$_POST["poste"],$_POST["description"],$_POST["image"]);
				$table="PHP_EQUIPE";
				
				if (strcmp($_SESSION["lang"],"fr")==0){
					parent::updateBD($_POST["id"], Donnees::$equipeFR ,$valeurs,$table);	
				}
				else{
					parent::updateBD($_POST["id"], Donnees::$equipeEN ,$valeurs,$table);
				}					
			}			
			
			else{
				return false;
			}			
			
		}
		
		
		public function ajouterBDCarrieres(){
			if (isset($_POST["poste"]) && isset($_POST["horaire"]) && isset($_POST["ajouter"])){
				$valeurs=array($_POST["poste"],$_POST["horaire"],$_POST["embauche"]);
				$table="PHP_CARRIERES";
				
				if (strcmp($_SESSION["lang"],"fr")==0){
					parent::insertBD(Donnees::$carrieresFR ,$valeurs,$table);	
				}
				else{
					parent::insertBD(Donnees::$carrieresEN ,$valeurs,$table);
				}					
			}
			
			else{
				return false;
			}
		}
		
		public function updateBDCarrieres(){
			if (isset($_POST["poste"]) && isset($_POST["horaire"]) && isset($_POST["id"])){
				$valeurs=array($_POST["id"],$_POST["poste"],$_POST["horaire"],$_POST["embauche"]);
				$table="PHP_CARRIERES";
				
				if (strcmp($_SESSION["lang"],"fr")==0){
					parent::updateBD($_POST["id"], Donnees::$carrieresFR ,$valeurs,$table);	
				}
				else{
					parent::updateBD($_POST["id"], Donnees::$carrieresEN ,$valeurs,$table);
				}					
			}
			
			else{
				return false;
			}
		}
		
		public function ajouterBDServices(){
			if (isset($_POST["table"]) && isset($_POST["ajouter"])){
				if (strcmp($_POST["table"],"services")==0){
					$valeurs=array($_POST["nom"],$_POST["description"]);
					$table="PHP_SERVICES";
					if (strcmp($_SESSION["lang"],"fr")==0){
						parent::insertBD(Donnees::$servicesFR ,$valeurs,$table);	
					}
					else{
						parent::insertBD(Donnees::$servicesEN ,$valeurs,$table);
					}					
				}
			}
			else{
				return false;
			}
		}
		
		public function updateBDServices(){
			if (isset($_POST["table"]) && isset($_POST["update"])){
				if (strcmp($_POST["table"],"services")==0){
					$valeurs=array($_POST["id"],$_POST["nom"],$_POST["description"]);
					$table="PHP_SERVICES";
					if (strcmp($_SESSION["lang"],"fr")==0){
						parent::updateBD($_POST["id"], Donnees::$servicesFR ,$valeurs,$table);	
					}
					else{
						parent::updateBD($_POST["id"], Donnees::$servicesEN ,$valeurs,$table);
					}	
				}				
			}
			
			else{
				return false;
			}
		}
		
	}